//
//  MQDiscoverSearchView.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/11.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
class MQDiscoverSearchView: UIView {
    @IBOutlet weak var textFieldRightConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var textField: UITextField!
    
    class func searchView() -> MQDiscoverSearchView{
       return NSBundle.mainBundle().loadNibNamed("MQDiscoverSearchView", owner: nil, options: nil).last as! MQDiscoverSearchView
    }
    
    @IBAction func cancelBtn(sender: UIButton) {
        printfLog("取消编辑")
        self.textField.resignFirstResponder()
        self.textFieldRightConstraint.constant = 0

        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func awakeFromNib() {
        let leftView = UIImageView()
        leftView.image = UIImage(named: "searchbar_textfield_search_icon")
        leftView.frame.size = CGSizeMake(frame.height, frame.height)
        textField.leftView = leftView
        leftView.contentMode = .Center
        textField.leftViewMode = .Always
        
//        textField.layer.borderWidth = 2
//        textField.layer.borderColor = UIColor.purpleColor().CGColor
//        textField.layer.cornerRadius = 10
        
    }
    
    @IBAction func textFieldEditingBegin(sender: AnyObject) {
        printfLog("textField开始编辑")
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textFieldRightConstraint.constant = self.cancelBtn.frame.size.width
            self.layoutIfNeeded()
        }
    }
}
